﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Threading;

namespace SQLDataTransferer.ViewModel
{
    public class MainWindowViewModel : BaseViewModel
    {
        #region Fields

        ObservableCollection<ViewModel.BaseViewModel> _workspaces;

        // Ribbon
        RelayCommand _transferCommand;
        RelayCommand _attachCommand;
        RelayCommand _detachCommand;
        RelayCommand _settingsCommand;

        #endregion

        #region Constructor(s)

        public MainWindowViewModel(System.Globalization.CultureInfo culture, Dispatcher dispatcher, Window main)
        {
            base.CurrentCulture = culture;
            base.DispatcherObject = dispatcher;
            base.MainWindow = main;
            //TODO:

            //// Set Theme
            //this.SetTheme(null);

            // Load default Tab
            this.AddTransferModel();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Returns the collection of available workspaces to display.
        /// A 'workspace' is a ViewModel that can request to be closed.
        /// </summary>
        public ObservableCollection<ViewModel.BaseViewModel> Workspaces
        {
            get
            {
                if (this._workspaces == null)
                {
                    this._workspaces = new ObservableCollection<ViewModel.BaseViewModel>();
                    this._workspaces.CollectionChanged += this.OnWorkspacesChanged;
                }
                return this._workspaces;
            }
        }

        #region Commands

        public ICommand TransferCommand
        {
            get
            {
                if (this._transferCommand == null)
                    this._transferCommand = new RelayCommand((o) => this.AddTransferModel());

                return this._transferCommand;
            }
        }

        public ICommand AttachCommand
        {
            get
            {
                if (this._attachCommand == null)
                    this._attachCommand = new RelayCommand((o) => this.AddAttachModel());

                return this._attachCommand;
            }
        }

        public ICommand DetachCommand
        {
            get
            {
                if (this._detachCommand == null)
                    this._detachCommand = new RelayCommand((o) => this.AddDetachModel());

                return this._detachCommand;
            }
        }

        public ICommand SettingsCommand
        {
            get
            {
                if (this._settingsCommand == null)
                    this._settingsCommand = new RelayCommand((o) => this.AddSettingsModel());

                return this._settingsCommand;
            }
        }

        #endregion

        #endregion

        #region Methods

        private void AddTransferModel()
        {
            //Allow only single workspace
            ViewModel.TransferViewModel workspace =
                this.Workspaces.FirstOrDefault(vm => vm is ViewModel.TransferViewModel) as ViewModel.TransferViewModel;

            if (workspace == null)
            {
                workspace = new TransferViewModel("Transfer",
                    base.CurrentCulture,
                    base.DispatcherObject,
                    base.MainWindow);

                this.Workspaces.Add(workspace);
            }

            this.SetActiveWorkspace(workspace);
        }

        private void AddDetachModel()
        {
            //Allow only single workspace
            ViewModel.DetachDatabaseViewModel workspace =
                this.Workspaces.FirstOrDefault(vm => vm is ViewModel.DetachDatabaseViewModel) as ViewModel.DetachDatabaseViewModel;

            if (workspace == null)
            {
                workspace = new DetachDatabaseViewModel("Detach",
                    base.CurrentCulture,
                    base.DispatcherObject,
                    base.MainWindow);

                this.Workspaces.Add(workspace);
            }

            this.SetActiveWorkspace(workspace);
        }

        private void AddAttachModel()
        {
            //Allow only single workspace
            ViewModel.AttachDatabaseViewModel workspace =
                this.Workspaces.FirstOrDefault(vm => vm is ViewModel.AttachDatabaseViewModel) as ViewModel.AttachDatabaseViewModel;

            if (workspace == null)
            {
                workspace = new AttachDatabaseViewModel("Attach",
                    base.CurrentCulture,
                    base.DispatcherObject,
                    base.MainWindow);

                this.Workspaces.Add(workspace);
            }

            this.SetActiveWorkspace(workspace);
        }

        private void AddSettingsModel() 
        {
            //Allow only single workspace
            ViewModel.SettingsViewModel workspace =
                this.Workspaces.FirstOrDefault(vm => vm is ViewModel.SettingsViewModel) as ViewModel.SettingsViewModel;

            if (workspace == null)
            {
                workspace = new SettingsViewModel("Settings",
                    base.CurrentCulture,
                    base.DispatcherObject,
                    base.MainWindow);

                this.Workspaces.Add(workspace);
            }

            this.SetActiveWorkspace(workspace);
        }

        void SetActiveWorkspace(ViewModel.BaseViewModel workspace)
        {
            ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.Workspaces);
            if (collectionView != null)
                collectionView.MoveCurrentTo(workspace);
        }

        #endregion

        #region EventHandlers

        private void OnWorkspacesChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null && e.NewItems.Count != 0)
                foreach (ViewModel.BaseViewModel workspace in e.NewItems)
                    workspace.RequestClose += this.OnWorkspaceRequestClose;

            if (e.OldItems != null && e.OldItems.Count != 0)
                foreach (ViewModel.BaseViewModel workspace in e.OldItems)
                    workspace.RequestClose -= this.OnWorkspaceRequestClose;
        }

        private void OnWorkspaceRequestClose(object sender, EventArgs e)
        {
            ViewModel.BaseViewModel workspace = sender as ViewModel.BaseViewModel;
            workspace.Dispose();
            this.Workspaces.Remove(workspace);
        }

        #endregion
    }
}
